package dao.impl;

import bean.CateInfo;
import bean.CateLevelOne;
import dao.CateDao;
import util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CateDaoImpl implements CateDao {
    @Override
    public List<CateInfo> cate() {
        DBUtil.connect();
        ResultSet rs=null;
        List<CateInfo> cateInfos=new ArrayList<>();
        try {
            rs=DBUtil.executeQuery("select * from cateinfo");
            while(rs.next()){
                CateInfo cateInfo=new CateInfo();
                cateInfo.setId(rs.getInt("id"));
                cateInfo.setCateName(rs.getString("cateName"));
                cateInfo.setDes(rs.getString("des"));
                cateInfo.setParentId(rs.getInt("parentId"));
                cateInfos.add(cateInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return cateInfos ;
    }

    @Override
    public void update(CateInfo cateInfo) {
        DBUtil.connect();
        DBUtil.open();
        String sql="update cateinfo set cateName=? where id=? ";
        try {
            DBUtil.executeUpdate(sql,cateInfo.getCateName(),cateInfo.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            DBUtil.rollback();
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }

    @Override
    public void delete(Integer id) {
        DBUtil.connect();
        DBUtil.open();
        try {
            DBUtil.executeUpdate("delete from cateinfo where id=?",id);
        } catch (SQLException e) {
            e.printStackTrace();
            DBUtil.rollback();
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }

    }
    @Override
    public Integer bigadd(CateInfo cateInfo) {
        DBUtil.connect();
        DBUtil.open();
        String sql="insert into cateinfo (cateName,des,parentId) values(?,?,?)";
        try {
            return DBUtil.executeInsertOfGeneratedKey(sql,cateInfo.getCateName(),cateInfo.getDes(),cateInfo.getParentId());
        } catch (SQLException e) {
            e.printStackTrace();
            DBUtil.rollback();
            return 0;
        }finally {
            DBUtil.commit();
            DBUtil.close();
        }
    }

    @Override
    public List<CateLevelOne> findf() {
        DBUtil.connect();
        ResultSet rs=null;
        List<CateLevelOne> cateLevelOnes=new ArrayList<>();
        try {
            rs=DBUtil.executeQuery("select * from cateinfo where parentId = 0");
            while(rs.next()){
                CateLevelOne cateLevelOne=new CateLevelOne();
                cateLevelOne.setId(rs.getInt("id"));
                cateLevelOne.setCateName(rs.getString("cateName"));
                cateLevelOnes.add(cateLevelOne);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return cateLevelOnes ;
    }
}
